package com.iotechn.unimall.core.util.wx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iotechn.unimall.core.util.HttpUtils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class WeChatTempleUtils {

    public static final String TOKENKEY ="access_token";

    public static final String EXPIRETIMEKEY ="expire_time";

    private static final String wxMiNiSecret = "3f0934b7c5cebb5a325dd4a359caf1e2";

    private static final String wxMiNiAppid = "wx26940a24adfcab48";

    public static final String pattern = "developer";

    public static final String cacheToken = "WeChatTempleKey";

    public static String getCacheToken(Long userId){
        return userId + cacheToken;
    }

    /**  用户openid */
    public static final String MK_TOUSER="touser";

    /**  模板id */
    public static final String MK_TEMPLATE="template_id";

    /**  跳转页面 */
    public static final String MK_PAGE="page";

    /**  小程序版本 */
    public static final String MK_MINIPROGRAM="miniprogram_state";

    /**
     *
     * @param datas
     *            Map<String,String> value1=new HashMap<>();
     *              value1.put("value","测试模板消息");
     *              mapdata.put("thing2",value1);
     *订单号
     * {{thing1.DATA}}
     *
     * 下单人
     * {{thing14.DATA}}
     *
     * 金额
     * {{amount4.DATA}}
     *
     *              key   thing2
     *
     *              value   测试模板消息
     *
     * @param sendData
     * @return
     */
    public static Map<String,Object> getNewOrderTmp(Map<String,String> datas, Map<String,String> sendData){
        Map<String,Object> mapSend=new HashMap<>();
        Map<String,Object> mapdata=new HashMap<>();
        //遍历data数据
        for (Map.Entry<String, String> entry : datas.entrySet()
        ) {
            Map<String,String> value=new HashMap<>();
            value.put("value",entry.getValue());
            mapdata.put(entry.getKey(),value);
        }
        mapSend.put("data",mapdata);
        mapSend.put("lang","zh_CN");
        mapSend.put("touser",sendData.get(MK_TOUSER));
        mapSend.put("template_id",sendData.get(MK_TEMPLATE));
        mapSend.put("page",sendData.get(MK_PAGE));
        mapSend.put("miniprogram_state",sendData.get(MK_MINIPROGRAM));
        String s = JSON.toJSONString(mapSend);
        System.out.println("<<微信模板消息<<<<<<<<<<<"+s);
        return mapSend;
    }

    /**
     * 获取access_token
     * @return
     */
    public static Map<String,Object> getaccessToken(){
        Map<String,Object> map =new HashMap<>();
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+wxMiNiAppid+"&secret="+wxMiNiSecret;
        String s =HttpClientUtils.doGet(url);
        JSONObject jsonObject = JSONObject.parseObject(s);
        String access_token = (String)jsonObject.get("access_token");
        map.put("access_token",access_token);
        int second = (int)jsonObject.get("expires_in");
        map.put("expire_time",second);
        return map;
    }

    /**
     * 发送订阅消息
     * @param mapSend
     * @return
     */
    public static String sendTempleMsg(Map<String,Object> mapSend,String access_token) throws UnsupportedEncodingException {
        String sendTemMsgUrl="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+access_token;
        sendTemMsgUrl = sendTemMsgUrl.replaceAll("\"","");
        String s1 = JSONObject.toJSONString(mapSend);
        String send = HttpUtils.doPostJson(sendTemMsgUrl, s1);
        return send;
    }


    public static String toUtf8(String str,String Encoding) throws UnsupportedEncodingException {
        return new String(str.getBytes(Encoding),"UTF-8");
    }

    public static String getEncoding(String str) {
        String encode = "GB2312";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是GB2312
                String s = encode;
                return s; //是的话，返回“GB2312“，以下代码同理
            }
        } catch (Exception exception) {
        }
        encode = "ISO-8859-1";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是ISO-8859-1
                String s1 = encode;
                return s1;
            }
        } catch (Exception exception1) {
        }
        encode = "UTF-8";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是UTF-8
                String s2 = encode;
                return s2;
            }
        } catch (Exception exception2) {
        }
        encode = "GBK";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是GBK
                String s3 = encode;
                return s3;
            }
        } catch (Exception exception3) {
        }
        return "";
    }
}
